﻿using Home.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Utilities;

namespace Home.Domain
{
    public class InitializeData
    {
        public void Start()
        {
            using (var ef=new Entities.EFDbContext())
            {
                XElement root = XElement.Load(Common.Current.Server.MapPath("~/App_Data/xml/InitializeData.xml"));
                foreach (var item in root.Element("Classification").Elements("Item"))
                {
                    Classification model = new Classification();
                    model.ClassName = item.Attribute("classname").Value;
                    model.Name = item.Attribute("name").Value;
                    if (ef.Classifications.Where(t=>t.Name==model.Name||t.ClassName==model.ClassName).Count()==0)
                    {
                        int sort = ef.Classifications.Where(t => t.ClassName == model.ClassName).Select(p => p.Sort).DefaultIfEmpty(0).Min();
                        model.Sort = sort + 1;//按照xml顺序
                        ef.Classifications.Add(model);
                        ef.SaveChanges();
                    }
                }
            }
        }
    }
}
